using System;
using System.Collections;
using System.Text;
using System.Data;
using GeneralLibs;

namespace Lists
{
    public class Kho : DoiTuong
    {
        public enum ThuocTinhKho
        {
            None = 0,
            Kho = 1,
            Quay = 2
        }

        #region Ma property
        public string Ma
        {
            get
            {
                return (string)_DataRow["Ma"];
            }
            set
            {
                _DataRow["Ma"] = value;
            }
        }
        #endregion

        #region Ten property
        public string Ten
        {
            get
            {
                return (string)_DataRow["Ten"];
            }
            set
            {
                _DataRow["Ten"] = value;
            }
        }
        #endregion

        #region DiaChi property
        public string DiaChi
        {
            get
            {
                return (string)_DataRow["DiaChi"];
            }
            set
            {
                _DataRow["DiaChi"] = value;
            }
        }
        #endregion

        #region ThuocTinh property
        public ThuocTinhKho ThuocTinh
        {
            get
            {
                return (ThuocTinhKho)_DataRow["ThuocTinh"];
            }
            set
            {
                _DataRow["ThuocTinh"] = value;
            }
        }
        #endregion

        public Kho(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucKho : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucKho _Instance;
        public static DanhMucKho Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucKho();
                    UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Ma"]);
                    _Instance.DataTable.Constraints.Add(uniqueConstraint);
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    _Instance.DataView.Sort = "Ma";
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Kho";
            }
        }
        #endregion 

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new Kho((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public Kho this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new Kho(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public Kho NewDoiTuong()
        {
            try
            {
                return new Kho(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public Kho GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new Kho(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public Kho GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucKho GetAll() {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
